// select_place.js
var utils = require('../../../utils/util.js')

Page({

  data: {
    whichPage: 1,//1：表示选择发货地 2：表示选择收货地
    list_city: [],// 城市列表
    pos_clickCity: -1,// 点击的城市的索引
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    this.setData({
      whichPage: options.whichPage
    });

    //获取当前城市列表
    wx.request({
      url: getApp().globalData.serverAddress + 'get_current_city',

      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },

      method: "POST",

      success: function (res) {
        that.setData({
          list_city: res.data.info,
        });
      }
    });
  },

  /**
   * 城市列表的item被点击
   */
  showArea: function (e) {
    var that = this;

    var pos_clickCity = e.currentTarget.dataset.id;
    var list_city = that.data.list_city;

    //渲染点击位置的View
    var temp = [];
    var code_clickCity = list_city[pos_clickCity].cityCode;// 点击位置的城市的code
    for (var i = 0; i < list_city.length; i++) {
      var tempObj = list_city[i];
      if (code_clickCity == tempObj.cityCode) {
        temp[i] = {
          id: tempObj.id,
          changeColor: true,
          cityName: tempObj.cityName,
          cityCode: tempObj.cityCode,
          provinceName: tempObj.provinceName,
          provinceCode: tempObj.provinceCode
        };
      } else {
        temp[i] = {
          id: tempObj.id,
          changeColor: false,
          cityName: tempObj.cityName,
          cityCode: tempObj.cityCode,
          provinceName: tempObj.provinceName,
          provinceCode: tempObj.provinceCode
        };
      }
    }

    that.setData({
      pos_clickCity: pos_clickCity,
      list_city: temp
    });

    //获取已选择城市的区县列表
    if (-1 < pos_clickCity) {
      wx.request({
        url: getApp().globalData.serverAddress + 'get_area_by_city_code',

        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },

        method: "POST",

        data: utils.json2Form({
          cityCode: list_city[pos_clickCity].cityCode,
        }),

        success: function (res) {
          that.setData({
            list_area: res.data.info,
          });
        }
      });
    }
  },

  /**
   * 区县列表被点击
   */
  back2PrePage: function (e) {
    var that = this;

    var pos_clickArea = e.currentTarget.dataset.id;
    var list_area = that.data.list_area;

    //在返回上一个页面时，将数据回传回去
    var pos_clickCity = that.data.pos_clickCity;
    var list_city = that.data.list_city;
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2]; //上一个页面
    prevPage.setData({
      // address: {
      province: {
        name: list_city[pos_clickCity].provinceName,
        code: list_city[pos_clickCity].provinceCode,
      },
      city: {
        name: list_city[pos_clickCity].cityName,
        code: list_city[pos_clickCity].cityCode,
      },
      area: {
        name: list_area[pos_clickArea].areaName,
        code: list_area[pos_clickArea].areaCode,
      },
      // },
      // showArea: list_city[pos_clickCity].cityName + ' - ' + list_area[pos_clickArea].areaName,
    });

    console.log(prevPage.data.address)

    // if (1 == that.data.whichPage) {
    //   prevPage.setData({
    //     fromAddress: {
    //       province: {
    //         name: list_city[pos_clickCity].provinceName,
    //         code: list_city[pos_clickCity].provinceCode
    //       },
    //       city: {
    //         name: list_city[pos_clickCity].cityName,
    //         code: list_city[pos_clickCity].cityCode
    //       },
    //       area: list_area[pos_clickArea]
    //     },
    //   });
    // } else if (2 == that.data.whichPage) {
    //   prevPage.setData({
    //     toAddress: {
    //       province: {
    //         name: list_city[pos_clickCity].provinceName,
    //         code: list_city[pos_clickCity].provinceCode
    //       },
    //       city: {
    //         name: list_city[pos_clickCity].cityName,
    //         code: list_city[pos_clickCity].cityCode
    //       },
    //       area: list_area[pos_clickArea]
    //     },
    //   });
    // }

    //返回上一个页面
    wx.navigateBack();
  },
})